package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;

    /**
     * 登录功能
     *
     * @param request
     * @param employee
     * @return
     */
    @PostMapping("/login")
    public R<Employee> login(HttpServletRequest request, @RequestBody Employee employee) {
        //  获取传入的password
        String password = employee.getPassword();
        //对password进行加密
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        //使用mybatis QueryWrapper 进行条件查询
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        //设置查询条件 传入的employee的username在数据库里会否存在
        queryWrapper.eq(Employee::getUsername, employee.getUsername());
        //queryWrapper.eq(Employee::getPassword,password);
        //查询
        Employee emp = employeeService.getOne(queryWrapper);
        //如果查询结果不存在 返回登录失败
        if (emp == null) {
            return R.error("登录失败");
        }
        //如果用户存在,但输入的密码不正确 返回失败结果
        if (!emp.getPassword().equals(password)) {
            return R.error("密码输入错误,请重新输入");

        }
        //如果用户被禁用(数据库中的权限参数)则返回失败结果
        if (emp.getStatus() == 0) {
            return R.error("权限不足");
        }
        //用session保存employee的id
        request.getSession().setAttribute("employee", emp.getId());
        //设置密码为null 让前台获取不到密码的值
        emp.setPassword(null);
        //登陆成功,将emp对象返回到前台
        return R.success(emp);

    }

    /**
     * 退出功能
     *
     * @param request
     * @return
     */
    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest request) {
        request.getSession().removeAttribute("employee");
        return R.success("退出成功");
    }
    /*
        登录功能的完善
            使用过滤器对未登陆了的人不让访问
                LoginCheckFilter
     */

    //新增员工
    @PostMapping
    public R<String> save(HttpServletRequest req,@RequestBody Employee employee){
        //设置默认密码 对密码进行MD5加密
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        //获取更新时间和创建时间

        employeeService.save(employee);

        return R.success("添加成功");
    }

    /**
     * 分页员工查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(int page, int pageSize, String name) {
        log.info("page={},pageSize={},name={}", page, pageSize, name);
        Page pageinfo=new Page(page,pageSize);

        LambdaQueryWrapper<Employee> queryWrapper=new LambdaQueryWrapper();
        queryWrapper.like(StringUtils.isNotEmpty(name),Employee::getName,name);
        employeeService.page(pageinfo,queryWrapper);

        return R.success(pageinfo);
    }

    @PutMapping
    public R<String> update(HttpServletRequest req,@RequestBody Employee employee){
        employeeService.updateById(employee);
        return R.success("修改成功");
    }


    @GetMapping("/{id}")
    public R<Employee> getById(@PathVariable Long id){
        Employee emp = employeeService.getById(id);
        return R.success(emp);
    }

}
